﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;


/* Copyright (c) 2014
 * author: Ricky Kratochwil
 * @ date: 2014-06-25 (revised)
 * @ summary: Main entry point for application
*/

namespace GameOfCoins
{
	public partial class CMainMenu : Form
	{
		private PrivateFontCollection m_oCustFontCol;

		public CMainMenu()
		{
			InitializeComponent();

			m_oCustFontCol = new PrivateFontCollection();
			ScaleCtrls();
		}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// UTILITIES

		//Scale lable text and (try) to set correct position
		// - Lable text and positions are dependent from background image, which can have different scales depending on screen sesolution
		// - Use brute force to determin maximum text width an position
		// - Positions can be calculated up to a certain amount of error, for the purpose of the task I assume this to be sufficient
		// - Titel uses own TrueType Font, for portability the font will be loaded at runtime
		//@ KR / 2014.06.25
		private void ScaleCtrls()
		{
			int			iFontSize;
			SizeF		oSize;
			Graphics	g = CreateGraphics();

			//////////
			//Main titel
			m_oCustFontCol.AddFontFile("data\\TitleFont.ttf");
			iFontSize = 0;
			do
			{
				oSize = g.MeasureString(lblTitel.Text, new Font(lblTitel.Font.FontFamily, ++iFontSize));
			}
			while(oSize.Width < Width*0.75);
			lblTitel.Font = new Font(m_oCustFontCol.Families[0], iFontSize);

			//////////
			//Start Game
			iFontSize = 0;
			do
			{
				oSize = g.MeasureString(lblStartGame.Text, new Font(lblStartGame.Font.FontFamily, ++iFontSize));
			}
			while(oSize.Width < Width/4.8);
			lblStartGame.Font = new Font(lblStartGame.Font.FontFamily, iFontSize);
			lblStartGame.Location = new Point((int)((Width/5.48) - (lblStartGame.Width/2)),
											  (int)((Height/1.92) - (lblStartGame.Height/2)));

			//////////
			//World-Editor
			iFontSize = 0;
			do
			{
				oSize = g.MeasureString(lblWorldEditor.Text, new Font(lblWorldEditor.Font.FontFamily, ++iFontSize));
			}
			while(oSize.Width < Width/4.8);
			lblWorldEditor.Font = new Font(lblWorldEditor.Font.FontFamily, iFontSize);
			lblWorldEditor.Location = new Point((int)((Width/5.48) - (lblWorldEditor.Width/2)),
											  (int)((Height/1.48) - (lblWorldEditor.Height/2)));

			//////////
			//Quit
			iFontSize = 0;
			do
			{
				oSize = g.MeasureString(lblQuit.Text, new Font(lblWorldEditor.Font.FontFamily, ++iFontSize));
			}
			while(oSize.Width < Width/4.8);
			lblQuit.Font = new Font(lblQuit.Font.FontFamily, iFontSize);
			lblQuit.Location = new Point((int)((Width/5.48) - (lblQuit.Width/2)),
											  (int)((Height/1.12) - (lblQuit.Height/2)));

			g.Dispose();
		}

		//Confirm and close if needed
		//@ KR / 2014.06.25
		private void CloseApp()
		{
			if(MessageBox.Show("Wollen Sie 'Game Of Coins' wirklich beenden?", "Beenden", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				Close();
		}


//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// QUIT APP

		private void lblQuit_Click(object sender, EventArgs e)
		{
			CloseApp();
		}

		//Check for user input
		// - Requires KeyPreview = true
		//@ KR / 2014.06.25
		private void CMainMenu_KeyDown(object sender, KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Escape)	//Close App on Esc
				CloseApp();
		}


//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// CTRLS

		private void btnDebug_Click(object sender, EventArgs e)
		{
			Form1 oDebugForum = new Form1();
			oDebugForum.ShowDialog();
		}

		private void lblWorldEditor_Click(object sender, EventArgs e)
		{
			CWorldEditor oWordEditor = new CWorldEditor();
			oWordEditor.ShowDialog();
		}

		private void lblStartGame_Click(object sender, EventArgs e)
		{
			GameMainFrame oMainGame = new GameMainFrame();
			oMainGame.ShowDialog();
		}

	}
}
